﻿using System;
using System.Collections.Generic;
#if DEBUG
using System.Diagnostics;
#endif // #if DEBUG
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using FRL.DebtChased.Repository;
using FRL.DebtChased.Silverlight.Library;
using FRL.DebtChased.Silverlight.Library.RichContentService;

namespace FRL.DebtChased.ClientEditor
{

	/// <summary>
	/// Declaration of ManagerCtrl class
	/// Control for editing the client's personnel contacts
	/// </summary>
	public partial class ManagerCtrl : UserControl
	{

		/////////////////////////////////////////////////////////////
		// Attributes

		// List of comboboxes that show contacts
		private List<ComboBox> m_contactComboBoxes;

		private bool m_bBlockComboEvents = true;

		/////////////////////////////////////////////////////////////
		// Construction

		public ManagerCtrl()
		{

			// Load xaml
			InitializeComponent();

			// Retain a list of combo boxes that have contacts
			m_contactComboBoxes = new List<ComboBox>();
			m_contactComboBoxes.Add(cboSalesPerson);
			m_contactComboBoxes.Add(cboCommsContact);
			m_contactComboBoxes.Add(cboCommsContactCCA);
			m_contactComboBoxes.Add(cboCommsContactCCB);
			m_contactComboBoxes.Add(cboHolidayContact);
			m_contactComboBoxes.Add(cboHolidayContactCC);
			_reloadContacts(null, null);

			// Comms type
			ComboBoxObject.LoadFromEnumeration(cboCommsType, typeof(Definitions.eClientCommsType));
			cboCommsType.SelectedIndex = 0;
			ComboBoxObject.LoadFromEnumeration(cboCommsFrequency, typeof(Definitions.eClientCommsFrequency));
			cboCommsFrequency.SelectedIndex = 0;

			// Reset flag
			m_bBlockComboEvents = false;

		}

		private void UserControl_Loaded(object sender, RoutedEventArgs e)
		{

			// If not in design mode...
			if (
				(App.Current != null)
				&&
				(App.Current.RootVisual != null)
				&&
				((App.Current.RootVisual as MainPage) != null)
			)
			{

				// Need to know when contacts list has changed
                (App.Current.RootVisual as MainPage).ClientEditor.tabContacts.ContactListChange += new EventHandler(TabContacts_ContactListChange);

			}

		}

		/////////////////////////////////////////////////////////////
		// Events

		private void butRequestModification_Click(object sender, EventArgs e)
		{

			// Compare buttons
			Definitions.eDataField dataField = Definitions.eDataField.NotSet;
			if (sender == butModifySalesPerson)
				dataField = Definitions.eDataField.Client_SalesPerson;
			else if (sender == butModifyTeamGroup)
				dataField = Definitions.eDataField.Client_TeamGroup;
			else if (sender == butModifyCommsFrequency)
				dataField = Definitions.eDataField.Client_CommsFrequency;
			else if (sender == butModifyCommsType)
				dataField = Definitions.eDataField.Client_CommsType;
			else if (sender == butModifyCommsContact)
				dataField = Definitions.eDataField.Client_CommsContact;
			else if (sender == butModifyCommsContactCCA)
				dataField = Definitions.eDataField.Client_CommsContactCCA;
			else if (sender == butModifyCommsContactCCB)
				dataField = Definitions.eDataField.Client_CommsContactCCB;
			else if (sender == butModifyHolidayContact)
				dataField = Definitions.eDataField.Client_HolidayContact;
#if DEBUG
			else
				Debug.Assert(false);
#endif // #if DEBUG

			// Show modification request window
			RequestModificationWnd wnd = new RequestModificationWnd(dataField);
			wnd.Show();

		}

		void TabContacts_ContactListChange(object sender, EventArgs e)
		{

			// Get the contacts map
			Dictionary<int, string> contactsMap = sender as Dictionary<int, string>;

			// Loop through combo boxes
			_reloadContacts(contactsMap, null);

		}

		private void cboContact_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{

			// Shortcuts
			if (m_bBlockComboEvents)
				return;

			// Get the contact list
            Dictionary<int, string> contactsMap = (App.Current.RootVisual as MainPage).ClientEditor.tabContacts.ContactMap;

			// Reload the contact controls
			_reloadContacts(contactsMap, sender as ComboBox);

		}

		/////////////////////////////////////////////////////////////
		// Operations

		private void _reloadContacts(Dictionary<int, string> contactsMap, ComboBox Ignore)
		{

			// Block updates
			m_bBlockComboEvents = true;
			{

				// Loop through combo boxes
				foreach (ComboBox ctrl in m_contactComboBoxes)
				{

					// Ignore the control that triggered the event
					if (ctrl == Ignore)
						continue;

					// Get the selected item
					int iSelectedItemData = 0;
					if (ctrl.SelectedItem != null)
						iSelectedItemData = (ctrl.SelectedItem as ComboBoxObject).ItemData;

					// Load the combo boxes
					ctrl.Items.Clear();
					ctrl.Items.Add(new ComboBoxObject(Definitions.NotSet, 0, null));
					if (contactsMap != null)
					{
						foreach (var di in contactsMap)
						{

							// Should this contact be added...
							bool bAdd = false;
							if (ctrl == cboCommsContact)
								bAdd = (
									(
										(cboCommsContactCCA.SelectedItem == null)
										||
										(di.Key != (cboCommsContactCCA.SelectedItem as ComboBoxObject).ItemData)
									)
									&&
									(
										(cboCommsContactCCB.SelectedItem == null)
										||
										(di.Key != (cboCommsContactCCB.SelectedItem as ComboBoxObject).ItemData)
									)
								);
							else if (ctrl == cboCommsContactCCA)
								bAdd = (
									(
										(cboCommsContact.SelectedItem == null)
										||
										(di.Key != (cboCommsContact.SelectedItem as ComboBoxObject).ItemData)
									)
									&&
									(
										(cboCommsContactCCB.SelectedItem == null)
										||
										(di.Key != (cboCommsContactCCB.SelectedItem as ComboBoxObject).ItemData)
									)
								);
							else if (ctrl == cboCommsContactCCB)
								bAdd = (
									(
										(cboCommsContactCCA.SelectedItem == null)
										||
										(di.Key != (cboCommsContactCCA.SelectedItem as ComboBoxObject).ItemData)
									)
									&&
									(
										(cboCommsContactCCB.SelectedItem == null)
										||
										(di.Key != (cboCommsContactCCB.SelectedItem as ComboBoxObject).ItemData)
									)
								);
							else if (ctrl == cboHolidayContact)
								bAdd = (
									(
										(cboCommsContact.SelectedItem == null)
										||
										(di.Key != (cboCommsContact.SelectedItem as ComboBoxObject).ItemData)
									)
									&&
									(
										(cboHolidayContactCC.SelectedItem == null)
										||
										(di.Key != (cboHolidayContactCC.SelectedItem as ComboBoxObject).ItemData)
									)
								);
							else if (ctrl == cboHolidayContactCC)
								bAdd = (
									(cboHolidayContact.SelectedItem == null)
									||
									(di.Key != (cboHolidayContact.SelectedItem as ComboBoxObject).ItemData)
								);
							else
								bAdd = true;

							// If adding contact...
							if (bAdd)
							{
								ComboBoxObject newCbo = new ComboBoxObject(di.Value, di.Key, null);
								ctrl.Items.Add(newCbo);
							}

						}

					}

					// Re-select contact
					if (ComboBoxObject.ContainsByItemData(ctrl, iSelectedItemData))
						ComboBoxObject.SelectByItemData(ctrl, iSelectedItemData);
					else
						ctrl.SelectedIndex = 0;

				}

			}
			m_bBlockComboEvents = false;

		}

		public void PostSupportDataLoaded()
		{

			// Called after plug-in has loaded support data

			// Load the user teams
			cboTeamGroup.Items.Add(new ComboBoxObject(Definitions.NotSet,0,null));
			foreach (var diLoop in CachedData.Singleton.FinanceTeams)
				cboTeamGroup.Items.Add(new ComboBoxObject(diLoop.Value, diLoop.Key, null));
			cboTeamGroup.SelectedIndex = 0;

			// Bail out in design mode
			if (CachedDataBase.SingletonBase == null)
				return;

			// Apply security to fields
			bool bAllowEditing = ((CachedDataBase.SingletonBase.UserSessionTransport.Roles & (int)(Definitions.eRoles.FIN_SYSADMIN | Definitions.eRoles.FIN_MNGADMIN)) != 0);
			if (bAllowEditing)
			{

				// Hide the modification buttons
				butModifySalesPerson.Visibility = System.Windows.Visibility.Collapsed;
				butModifyTeamGroup.Visibility = System.Windows.Visibility.Collapsed;
				butModifyCommsFrequency.Visibility = System.Windows.Visibility.Collapsed;
				butModifyCommsType.Visibility = System.Windows.Visibility.Collapsed;
				butModifyCommsContact.Visibility = System.Windows.Visibility.Collapsed;
				butModifyCommsContactCCA.Visibility = System.Windows.Visibility.Collapsed;
				butModifyCommsContactCCB.Visibility = System.Windows.Visibility.Collapsed;
				butModifyHolidayContact.Visibility = System.Windows.Visibility.Collapsed;

			}
			else
			{

				// Disable the combo boxes
				cboSalesPerson.IsEnabled = false;
				cboTeamGroup.IsEnabled = false;
				cboCommsFrequency.IsEnabled = false;
				cboCommsType.IsEnabled = false;
				cboCommsFrequency.IsEnabled = false;
				cboCommsContact.IsEnabled = false;
				cboCommsContactCCA.IsEnabled = false;
				cboCommsContactCCB.IsEnabled = false;
				cboHolidayContact.IsEnabled = false;
				chkHolidayActivated.IsEnabled = false;
				cboHolidayContactCC.IsEnabled = false;
				dtHolidayStart.IsEnabled = false;
				dtHolidayEnd.IsEnabled = false;

			}

		}

		public void Set(ClientTransport clientTransport)
		{

			// Set controls
			ComboBoxObject.SelectByItemData(cboSalesPerson, clientTransport.SalesContactId);
			ComboBoxObject.SelectByItemData(cboTeamGroup, clientTransport.FinanceTeamId);

			ComboBoxObject.SelectByItemData(cboCommsType, clientTransport.CommsType);
			ComboBoxObject.SelectByItemData(cboCommsFrequency, clientTransport.CommsFrequency);

			ComboBoxObject.SelectByItemData(cboCommsContact, clientTransport.CommsContactId);
			ComboBoxObject.SelectByItemData(cboCommsContactCCA, clientTransport.CommsContactCCIdA);
			ComboBoxObject.SelectByItemData(cboCommsContactCCB, clientTransport.CommsContactCCIdB);

			ComboBoxObject.SelectByItemData(cboHolidayContact, clientTransport.HolidayContactId);

			chkHolidayActivated.IsChecked = clientTransport.IsHolidayActivated;

			ComboBoxObject.SelectByItemData(cboHolidayContactCC, clientTransport.HolidayContactCCId);

			dtHolidayEnd.SelectedDate = clientTransport.HolidayEndAt;

		}

		public void Get( ClientTransport clientTransport, List<string> Errors )
		{

			// Populate the client object with details from the control
			// and find errors in the user input

			// Get controls
			clientTransport.SalesContactId = (cboSalesPerson.SelectedValue as ComboBoxObject).ItemData;

			clientTransport.FinanceTeamId = (cboTeamGroup.SelectedValue as ComboBoxObject).ItemData;

			clientTransport.CommsType = (cboCommsType.SelectedValue as ComboBoxObject).ItemData;
			clientTransport.CommsFrequency = (cboCommsFrequency.SelectedValue as ComboBoxObject).ItemData;

			clientTransport.CommsContactId = (cboCommsContact.SelectedValue as ComboBoxObject).ItemData;
			clientTransport.CommsContactCCIdA = (cboCommsContactCCA.SelectedValue as ComboBoxObject).ItemData;
			clientTransport.CommsContactCCIdB = (cboCommsContactCCB.SelectedValue as ComboBoxObject).ItemData;

			clientTransport.HolidayContactId = (cboHolidayContact.SelectedValue as ComboBoxObject).ItemData;

			clientTransport.IsHolidayActivated = chkHolidayActivated.IsChecked ?? false;

			clientTransport.HolidayContactCCId = (cboHolidayContactCC.SelectedValue as ComboBoxObject).ItemData;

			clientTransport.HolidayEndAt = dtHolidayEnd.SelectedDate;

		}

	}

}
